{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在爬取第1页!\n",
      "正在爬取第2页!\n",
      "正在爬取第3页!\n",
      "正在爬取第4页!\n",
      "正在爬取第5页!\n",
      "正在爬取第6页!\n",
      "正在爬取第7页!\n",
      "正在爬取第8页!\n",
      "正在爬取第9页!\n",
      "正在爬取第10页!\n",
      "正在爬取第11页!\n",
      "正在爬取第12页!\n",
      "正在爬取第13页!\n",
      "正在爬取第14页!\n",
      "正在爬取第15页!\n",
      "正在爬取第16页!\n",
      "正在爬取第17页!\n",
      "正在爬取第18页!\n",
      "正在爬取第19页!\n",
      "正在爬取第20页!\n",
      "正在爬取第21页!\n",
      "正在爬取第22页!\n",
      "正在爬取第23页!\n",
      "正在爬取第24页!\n",
      "正在爬取第25页!\n",
      "正在爬取第26页!\n",
      "正在爬取第27页!\n",
      "正在爬取第28页!\n",
      "正在爬取第29页!\n",
      "正在爬取第30页!\n",
      "正在爬取第31页!\n",
      "正在爬取第32页!\n",
      "正在爬取第33页!\n",
      "正在爬取第34页!\n",
      "正在爬取第35页!\n",
      "正在爬取第36页!\n",
      "正在爬取第37页!\n",
      "正在爬取第38页!\n",
      "正在爬取第39页!\n",
      "正在爬取第40页!\n",
      "正在爬取第41页!\n",
      "正在爬取第42页!\n",
      "正在爬取第43页!\n",
      "正在爬取第44页!\n",
      "正在爬取第45页!\n",
      "正在爬取第46页!\n",
      "正在爬取第47页!\n",
      "正在爬取第48页!\n",
      "正在爬取第49页!\n",
      "正在爬取第50页!\n",
      "Over!\n"
     ]
    }
   ],
   "source": [
    "import csv\n",
    "import requests\n",
    "import time\n",
    "\n",
    "def trans_date(v_timestamp):\n",
    "    \"\"\"10位时间戳转换为时间字符串\"\"\"\n",
    "    v_timestamp = float(v_timestamp)\n",
    "    timeArray = time.localtime(v_timestamp)\n",
    "    otherStyleTime = time.strftime(\"%Y-%m-%d %H:%M:%S\", timeArray)\n",
    "    return otherStyleTime\n",
    " \n",
    "def tran_gender(gender_tag):\n",
    "    \"\"\"转换性别\"\"\"\n",
    "    if gender_tag == 1:\n",
    "        return '男'\n",
    "    elif gender_tag == 0:\n",
    "        return '女'\n",
    "    else:  # -1\n",
    "        return '未知'\n",
    " \n",
    "def fecth_everything(comment):\n",
    "    gender = tran_gender(comment[\"author\"][\"member\"][\"gender\"])\n",
    "    # 评论的时间\n",
    "    time_un = comment[\"created_time\"]\n",
    "    publish_time = trans_date(time_un)\n",
    "    # 评论的IP属地\n",
    "    IP = comment[\"address_text\"]\n",
    "    # 评论的内容\n",
    "    content = comment[\"content\"]\n",
    "    # 评论的点赞数\n",
    "    num = comment[\"vote_count\"]\n",
    " \n",
    "    return gender, publish_time, IP, content, num\n",
    " \n",
    "# TODO:输入 answer 号\n",
    "answer_id = 101127202379\n",
    "# TODO:添加你的 User-Agent\n",
    "ua = \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36\"\n",
    "# TODO:添加你的 cookie\n",
    "c = \"_zap=9f6cf330-c057-48d2-aa92-a5bdd5caf3e0; d_c0=AGCSyd2lGhmPTtTOp24tuOQsySL7P6zWC7E=|1724065040; _xsrf=SFpbkm5WoXiWvHIS1kXkD0I4dXSsRygl; q_c1=d37c6486789d44fa804bdf9fbc178d3e|1725438335000|1725438335000; z_c0=2|1:0|10:1741063331|4:z_c0|80:MS4xT3N4NklnQUFBQUFtQUFBQVlBSlZUWkRfbm1pUmR0N19SRWdlUEFkbjE5NlhWYi1LcDJrUjN3PT0=|7a66c8887c5ce0570c6addab9c424918bb325f023c7fa0da0a003ffcd3c4442d; __zse_ck=004_1Ld4GIbcBllzt17HlaG=09W2MY=p3Osa5cpWwW66Cx52d/WpXHaVMm5xmE9n3YYhiNExkbqJfWlprg=tNabvZD2uzQDLLQuJ4b3fSJNW8OEGY2ozzKR35qP2F8aWygAK-DKbJhIDjVXvOuf1A16Cpy2tY7Ps6L41nyq8qiR6zXU+12qH4rr33eqyhGDvf2vQCuFctyE7mK6BP4i2VqLJ9pLlbJHCWB6rcaB+kDW7PtauMcUM2N+zCYAHJVhjQq5nZ; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1741488370,1741516100,1741584957,1741590796; HMACCOUNT=96F1716901CA32EC; tst=r; SESSIONID=9e9tzAfzpf96e8xpLWQ0n4W9hITLlznsOdHGnBsg2iz; JOID=VFgRC0hZXOMis_VFKFffOQDfWFk9Zj3WXcyyJ14sPN4Y3cg7XNacKkuw900vzLno0Le51IG3g7JVeNx8GUBJifM=; osd=VV4SC0hYWuAis_RDK1ffOAbcWFk8YD7WXc20JF4sPdgb3cg6WtWcKkq29E0vzb_r0Le40oK3g7NTe9x8GEZKifM=; edu_user_uuid=edu-v1|5b418fd0-94ab-41dd-96b3-0753edaec7f2; BEC=b7b0f394f3fd074c6bdd2ebbdd598b4e; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1741607462\"\n",
    "# 定义参数\n",
    "headers = {\n",
    "    'user-agent': ua\n",
    "}\n",
    "cookies = {\n",
    "    # 填自己的z_0 cookie\n",
    "    'cookie': c\n",
    "}\n",
    " \n",
    "# 加载 csv 文件\n",
    "f = open(\"Kimi算不算国内的顶级AI.csv\", mode=\"a\", encoding=\"utf-8-sig\")\n",
    "csv_write = csv.writer(f)\n",
    " \n",
    "# 开始爬取\n",
    "i = 0\n",
    "j = 1\n",
    "offset = 0\n",
    "while i == 0:\n",
    "    # 获取数据\n",
    "    url = f\"https://www.zhihu.com/api/v4/answers/{answer_id}/root_comments?order=normal&limit=20&offset={offset}&status=open\"\n",
    "    resp = requests.get(url, headers=headers, cookies=cookies)\n",
    "    data = resp.json()\n",
    "    # 无数据时终止循环\n",
    "    if data[\"data\"] == None:\n",
    "        break\n",
    "    print(f\"正在爬取第{j}页!\")\n",
    "    comments = data[\"data\"]\n",
    "    for comment in comments:\n",
    "        # 获取信息\n",
    "        gender, publish_time, IP, content, num = fecth_everything(comment)\n",
    "        # 存文件\n",
    "        csv_write.writerow([gender, publish_time, IP, content, num])\n",
    " \n",
    "        # 读取子评论\n",
    "        if comment[\"child_comments\"]:\n",
    "            for child in comment[\"child_comments\"]:\n",
    "                gender, publish_time, IP, content, num = fecth_everything(child)\n",
    "                # 存文件\n",
    "                csv_write.writerow([gender, publish_time, IP, content, num])\n",
    " \n",
    "    # 读取下一个 url\n",
    "    offset += 20\n",
    "    j += 1\n",
    "    # 终止读取\n",
    "    if offset == 1000:\n",
    "        break\n",
    " \n",
    "f.close()\n",
    "print(\"Over!\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
